/*
 * Powered By [rapid-framework]
 * Csdn Code: generate simple CRUD code in single table.
 * Since 2015 - 2017
 */

BUI.use('common/page');
BUI.use([ 'common/search', 'bui/overlay', 'bui/grid', 'bui/data', 'bui/form','bui/select' ],
    function(Search, Overlay, Grid, Data, Form,Select) {
        // 查询表单
        var form = new Form.Form({
            srcNode : '#searchForm'
        }).render();
        var items=[];
        var itemJson={};
        $.ajaxSettings.async = false;
        $.getJSON('showAllChannels',function (channels) {
            for(var i=0;i<channels.length;i++){
                items[i]={text:channels[i].channelDesc,value:channels[i].channel};
                toJson(itemJson,channels[i].channel,channels[i].channelDesc);
            }
        });
        $.ajaxSettings.async = true;
        var selectData = {'enumRenderer':'true','items':items};
        $("#channelName").attr('data-select',JSON.stringify(selectData));
        function toJson(array, prop, val) {
            // 如果 val 被忽略
            if (typeof val === "undefined") {
                // 删除属性
                delete array[prop];
            } else {
                // 添加 或 修改
                if (val.length > 0) {
                    array[prop] = val;
                }
            }
        }
        var select = new Select.Select({
            render:'#channelSelect',
            valueField:'#channel',
            items:items
        });
        select.render();
        // 定义表头
        columns = [ {
            title : '主键ID',
            dataIndex : 'channelCharacterId',
            visible : false
        },{
            title : '渠道',
            render: '.test',
            dataIndex : 'channel',
            elCls : 'center',
            width : 200,
            renderer: BUI.Grid.Format
                .enumRenderer(itemJson)
        },{
            title : '参数比例',
            dataIndex : 'discount',
            elCls : 'center',
            width : 100
        },{title:'操作',
            dataIndex:'',
            width:100,
            renderer : function(value,obj){
                var   editStr = '<span class="grid-command btn-edit" title="编辑信息">编辑</span>';
                return editStr;
            }
        }];
        // 编辑
        var editing = new Grid.Plugins.DialogEditing({
                contentId : 'content',
                triggerCls : 'btn-edit',
                editor : {
                    title : '渠道参数比例表',
                    width : 400
                }
            }),
            // 获取数据
            store = new BUI.Data.Store({
                url : 'listChannelCharacter',
                pageSize : 20,
                autoLoad : true,
                remoteSort : true
            });
        // 获取表格对象
        gridCfg = Search.createGridCfg(columns, {
            plugins : [ editing, Grid.Plugins.CheckSelection ],
            tbar : { // 添加
                items : [ {
                    btnCls : 'button button-small',
                    text : '<i class="icon-plus"></i>添加',
                    listeners : {
                        'click' : addFunction
                    }
                }, {
                    btnCls : 'button button-small',
                    text : '<i class="icon-refresh"></i>刷新',
                    listeners : {
                        'click' : refreshFunction
                    }
                }]
            }
        });
        search = new Search({
            width : '100%',// 这个属性一定要设置
            store : store,
            autoSearch : true,
            gridCfg : gridCfg
        });
        grid = search.get('grid');

        function addFunction() {
            var newData = {channel :'请选择渠道'};
            editing.add(newData);
        }
        // 刷新
        function refreshFunction() {
            search.load();
        }
        // 编辑提交
        editing.on('accept', function(ev) {
            var record = editing.get('record');
            if (record.hasOwnProperty("b"))
                delete record["b"];
            if (record.hasOwnProperty(""))
                delete record[""];
            var saveUrl = record["channelCharacterId"] == "" ? 'addChannelCharacter'
                : 'updateChannelCharacter';
            jQuery.ajax({
                type : 'POST',
                url : saveUrl,
                dataType : 'json',
                contentType : 'application/json;charset=UTF-8',
                data : JSON.stringify(record),
                complete : function(data) {
                    if (data.responseText == "success") { // 删除成功
                        search.load();
                    } else { // 删除失败
                        BUI.Message.Alert('保存失败！');
                    }
                }
            });
        });
    });
